package com.robam.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.robam.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.robam.gulimall.product.entity.AttrEntity;
import com.robam.gulimall.product.vo.AttrGroupAndAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.robam.gulimall.product.entity.AttrGroupEntity;
import com.robam.gulimall.product.service.AttrGroupService;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.R;



/**
 * 属性分组
 *
 * @author bobo
 * @email 412467850@qq.com
 * @date 2020-10-15 08:42:51
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @GetMapping("/page/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R page(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId){
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(catId == null || catId == 0) {
            wrapper = null;
        } else {
            wrapper.eq("catelog_id", catId);
        }
        PageUtils page = attrGroupService.queryPage(params, wrapper);
        return R.ok().put("page", page);
    }

    @GetMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@PathVariable("catId") Long catId){
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(catId == null || catId == 0) {
            wrapper = null;
        } else {
            wrapper.eq("catelog_id", catId);
        }
        List<AttrGroupEntity> list = attrGroupService.list(wrapper);
        return R.ok().put("list", list);
    }

    /**
     * 根据属性分组id查询分组关联的属性列表
     * @param groupId
     * @return
     */
    @GetMapping("/attr/relation/{groupId}")
    public R getAttrGroupAttrRelationByGroupId(@PathVariable("groupId") Long groupId) {
        List<AttrEntity> attrEntityList = attrGroupService.getAttrGroupAttrRelationByGroupId(groupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 根据属性分组id查询此分组还没有关联的属性列表
     * @return
     */
    @GetMapping("/attr/unrelation/{groupId}")
    public R getAttrGroupAttrNoRelation(@RequestParam Map<String, Object> params, @PathVariable("groupId") Long groupId) {
        PageUtils page = attrGroupService.getAttrGroupAttrNoRelation(params, groupId);
        return R.ok().put("page", page);
    }

    /**
     * 新建属性和属性分组关联
     * @param relationEntities
     * @return
     */
    @PostMapping("/attr/addrelation")
    public R addAttrGroupAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrGroupService.addAttrGroupAttrRelation(relationEntities);
        return R.ok();
    }

    /**
     * 根据三级分类id获取属性分组，及分组关联的属性
     * @param catId
     * @return
     */
    @GetMapping("/getAttrGroupAndAttrByCatId/{catId}")
    public R getAttrGroupAndAttrByCatId(@PathVariable("catId") Long catId) {
        List<AttrGroupAndAttrsVo> attrGroupAndAttrsVos = attrGroupService.getAttrGroupAndAttrByCatId(catId);
        return R.ok().put("data", attrGroupAndAttrsVos);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getAttrGroupInfo(attrGroupId);
		System.out.println(attrGroup);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
